package com.example.vv.model.register;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.RegisterEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.fragmentWork.utlis.InputTextHelper;
import com.example.vv.fragmentWork.view.CustomerTitleBar;
import com.example.vv.model.login.LoginActivity;
import com.example.vv.model.register.base.contract.RegisterContract;
import com.example.vv.model.register.base.presenter.RegisterPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends MvpActivity<RegisterContract.View, RegisterPresenter> implements RegisterContract.View {

    @BindView(R.id.title_bar)
    CustomerTitleBar titleBar;
    @BindView(R.id.user_et)
    EditText user_et;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.send_code_tv)
    TextView sendCodeTv;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.pwd_eye_iv)
    ImageView pwdEyeIv;
    @BindView(R.id.re_pwd_et)
    EditText rePwdEt;
    @BindView(R.id.re_eye_iv)
    ImageView reEyeIv;
    @BindView(R.id.register_tv)
    TextView registerTv;

    @Override
    public int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresenter initPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initView() {
        InputTextHelper.with(this)
                .setMain(registerTv)
                .addView(codeEt)
                .addView(user_et)
                .addView(pwdEt)
                .addView(rePwdEt)
                .setAlpha(true)
                .setListener(new InputTextHelper.OnInputTextListener() {
                    @Override
                    public boolean onInputChange(InputTextHelper helper) {
                        return presenter.isFormat();
                    }
                });
        titleBar.getLeft_img().setImageResource(R.mipmap.ic_back);
        titleBar.getLeft_img().setOnClickListener(v->{
            finish();
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }


    @Override
    public Context getMContext() {
        return this;
    }

    @Override
    public String getUserName() {
        return user_et.getText().toString().trim();
    }

    @Override
    public String getPassWord() {
        return pwdEt.getText().toString().trim();
    }

    @Override
    public String getRePassWord() {
        return rePwdEt.getText().toString().trim();
    }

    @Override
    public void getMsg(String msg) {
        if (msg.equals("")) {
            return;
        }else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setRegisterEntity(RegisterEntity registerEntity) {
        if (registerEntity!=null) {
            Integer integer = Integer.valueOf(registerEntity.getErrorCodeX());
            if (integer == 0) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                intent.putExtra("username", user_et.getText().toString().trim());
                setResult(99, intent);
                finish();
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @OnClick(R.id.register_tv)
    public void onViewClicked() {
        if (presenter.isFormat()){
            presenter.register();
        }
    }
}